
package com.insigma.business.qrcode.common.service.impl;

import com.google.common.collect.Lists;
import com.insigma.business.qrcode.common.dao.GovOrgDao;
import com.insigma.business.qrcode.common.dto.GovOrgBSearcher;
import com.insigma.business.qrcode.common.service.GovOrgService;
import com.insigma.business.qrcode.common.service.SequenceGenerator;
import com.insigma.business.qrcode.common.util.AdminAreaCodeUtils;
import com.insigma.business.qrcode.soldier.dto.SequenceNameConstant;
import com.insigma.business.qrcode.soldier.entity.DataStatsCDO;
import com.insigma.business.qrcode.soldier.entity.GovOrgDO;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;


import java.util.ArrayList;
import java.util.List;


@Service("govOrgService")
public class GovOrgServiceImpl implements GovOrgService {

    @Autowired(required = false)
    private GovOrgDao govOrgDao;

    @Autowired(required = false)
    private SequenceGenerator sequenceGenerator;

    private String baseId = "2020072800002109";

    
    @Override
    public List<GovOrgDO> search(final GovOrgBSearcher govOrgBSearcher) {
        if (Strings.isNotBlank(govOrgBSearcher.getAdminAreaCode())) {
            govOrgBSearcher.setAdminAreaCode(AdminAreaCodeUtils.getCutdAreaCode(govOrgBSearcher.getAdminAreaCode()));
        }
        return govOrgDao.search(govOrgBSearcher);
    }
   
    @Override
    public GovOrgDO get(final String id){
        return govOrgDao.get(id);
    }

    
    @Override
    public Integer insert(final GovOrgDO govOrgDO){
        govOrgDO.setId(sequenceGenerator.genKey(SequenceNameConstant.GOV_ORG_ID));
        return govOrgDao.insert(govOrgDO);
    }

    
    @Override
    public Integer update(final GovOrgDO govOrgDO){
        return govOrgDao.update(govOrgDO);
    }

    
    @Override
    public Integer delete(final String id, final String modifierType, final String modifierId){
        return govOrgDao.delete(id,modifierType,modifierId);
    }

    @Override
    public GovOrgDO getByAreaCodeAndId(String areaCode, String baseId) {
        return govOrgDao.getByAreaCodeAndId(areaCode, baseId);
    }

    @Override
    public Integer countSubFwz(String adminAreaCode) {
        GovOrgDO govOrgDO = govOrgDao.getByAreaCodeAndId(AdminAreaCodeUtils.fillAreaCode(adminAreaCode), baseId);
        if (govOrgDO == null) {
            return 0;
        }

        int count =  govOrgDao.countSubStationByCode(govOrgDO.getCode() + "%");
        return count;
    }

    @Override
    public List<GovOrgDO> listSubStationOfNextLevel(String adminAreaCode) {
        GovOrgDO govOrgDO = govOrgDao.getByAreaCodeAndId(AdminAreaCodeUtils.fillAreaCode(adminAreaCode), baseId);
        if (govOrgDO == null) {
            return new ArrayList<>();
        }
        List<GovOrgDO> list = govOrgDao.getByParentId(govOrgDO.getId());
        return list;
    }

    @Override
    public List<GovOrgDO> listStationByAdminAreaCode(String adminAreaCode) {
        GovOrgDO govOrgDO = govOrgDao.getByAreaCodeAndId(AdminAreaCodeUtils.fillAreaCode(adminAreaCode), baseId);
        if (govOrgDO == null) {
            return Lists.newArrayList();
        }
        return Lists.newArrayList(govOrgDO);
    }

    @Override
    public List<GovOrgDO> listFwzChildren(String code, int level) {
        return govOrgDao.listFwzChildren(code + "%", level);
    }

    @Override
    public List<GovOrgDO> searchAllSubOrgByCode(GovOrgBSearcher govOrgBSearcher) {
        return govOrgDao.searchAllSubOrgByCode(govOrgBSearcher);
    }

    @Override
    public List<GovOrgDO> listByEmployeeId(String creatorGovId) {
        return govOrgDao.listByEmployeeId(creatorGovId);
    }

    @Override
    public List<GovOrgDO> listSubStationByCode(String code) {
        return govOrgDao.listSubStationByCode(code);
    }

    @Override
    public List<DataStatsCDO> countSubFwzDistribution(String adminAreaCode) {
        GovOrgDO govOrgDO = govOrgDao.getByAreaCodeAndId(AdminAreaCodeUtils.fillAreaCode(adminAreaCode), baseId);

        if (govOrgDO == null || Strings.isBlank(govOrgDO.getLeafAdminAreaId())) {
            return Lists.newArrayList();
        }
        List<DataStatsCDO> result = new ArrayList<>();

        String code = govOrgDO.getCode();
        Integer nextLevel = govOrgDO.getLevel() + 1;
        List<GovOrgDO> list = govOrgDao.listFwzChildren(code + "%", nextLevel);
        if (!CollectionUtils.isEmpty(list)) {
            for (GovOrgDO orgDO : list) {
                String leafAdminAreaId = orgDO.getLeafAdminAreaId();
                if (Strings.isNotBlank(leafAdminAreaId)) {
                    int i = govOrgDao.countSubStationByCode(orgDO.getCode() + "%");
                    result.add(new DataStatsCDO(leafAdminAreaId, null, i));
                }
            }
        }

        return result;
    }

    @Override
    public String getAdminAreaCodeOfEmploy(String employeeId) {
        List<GovOrgDO> list = listByEmployeeId(employeeId);
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        return list.get(0).getLeafAdminAreaId();
    }

    @Override
    public GovOrgDO getGovOfEmploy(String employeeId) {
        List<GovOrgDO> list = listByEmployeeId(employeeId);
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        return list.get(0);
    }

    @Override
    public List<GovOrgDO> listByNameLikeOfYh(String name,  String code) {
        return govOrgDao.listByNameLikeOfYh(name, code);
    }

    @Override
    public GovOrgDO getFirstFwz(String adminAreaCode, String baseId) {
        return govOrgDao.getByAreaCodeAndId(adminAreaCode, baseId);
    }

    private int recursionGetSubCount(List<GovOrgDO> list) {
        if (CollectionUtils.isEmpty(list)) {
            return 0;
        }

        int count = list.size();
        for (GovOrgDO govOrgDO : list) {
            List<GovOrgDO> son = govOrgDao.getByParentId(govOrgDO.getId());
            count+=recursionGetSubCount(son);
        }

        return count;
    }

    public static void main(String[] args) {
        String a = "120.3038917520141,30.423147094270053";
        if (a.contains("|")) {
            String[] split = a.split("\\|");
            System.out.println();
        } else {
            String[] split = a.split(",");
            System.out.println();
        }
    }
}
